<template>
  <associate-goods
    ref="associateRef"
    :visible="visible"
    :formItems="formItems"
    :tableColumns="tableColumns"
    :fetchMethod="getAssociateList"
    :getAssociateKey="getAssociateKey"
    @updateVisible="updateVisible"
    @getResults="getAssociatedResults"
    @clearCategory="clearCategoryUuid"
  >
    <CategorySingleSelect
      checkStrictly
      slot="categorySelect"
      ref="categorySingle"
      @onSelected="setFormCateUuid"
    />
  </associate-goods>
</template>
<script>
import { mapMutations } from 'vuex';
import AssociateGoods from '../index';
import CategorySingleSelect from '@/components/Category/SingleSelect';
import { FORM_ITEMS, TABLE_COLUMNS } from './constants/add';
import { ASSOCIATE_TYPES_VALUES } from '@/components/AssociateGoods/constants';
import { getSearchParamBySearchType } from '@/utils/getUtils';
import { getBatchAddSpuListService } from '@/api/commodity/prepareSalePrice/prepareSpu';
export default {
  components: {
    AssociateGoods,
    CategorySingleSelect
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      formItems: FORM_ITEMS,
      tableColumns: TABLE_COLUMNS,
      getAssociateKey: row => row.spuNo
    }
  },
  beforeDestroy () {
    this.CLEAR_ROWS()
  },
  methods: {
    ...mapMutations('commonComps/associateGoods', ['DELETE_ROW', 'CLEAR_ROWS']),
    // 更新开关状态
    updateVisible (state) {
      this.$emit('update:visible', state)
    },
    // 获取关联结果
    getAssociatedResults (results) {
      const uuids = results.map(item => item.uuid)
      this.$emit('getUuids', uuids?.length == 0 ? null : uuids)
    },
    // 获取查询入参
    getSearchParams (infoData) {
      const { formData, paginationParams, associateState, mapValues } = infoData ?? {}
      const uuids = mapValues.map(item => item.uuid).join(',')
      const UuidItemsMaps = {
        [ASSOCIATE_TYPES_VALUES.Unassociated]: ['notInUuidStr', 'NotIN'],
        [ASSOCIATE_TYPES_VALUES.Associated]: ['uuidInStr', 'IN']
      }
      const [prop, searchType] = UuidItemsMaps[associateState]
      const searchParam = getSearchParamBySearchType(
        [...this.formItems, { prop, searchType }],
        { ...formData, [prop]: uuids }
      )
      const params = {
        searchParam: JSON.stringify(searchParam),
        ...paginationParams
      }
      return params
    },
    // 获取关联商品集合
    async getAssociateList (infoData, loadingFn) {
      const data = this.getSearchParams(infoData)
      const res = await getBatchAddSpuListService(data, loadingFn)
      if (res?.retData) return res.retData
    },
    // 设置表单分类编码
    setFormCateUuid (categoryInfo) {
      this.$refs?.associateRef?.setFormCategoryData('categoryUuid', categoryInfo?.categoryUuid)
    },
    // 清空表单分类
    clearCategoryUuid () {
      // eslint-disable-next-line no-unused-expressions
      this.$refs?.categorySingle?.clearCategoryNodes() // 清空单选分类
    },
    // 删除关联商品
    deleteGoods (row) {
      this.DELETE_ROW([row, this.getAssociateKey])
    }
  }
}
</script>
